package com.lzq.springbook.controller;

import com.lzq.springbook.model.UserInfo;
import com.lzq.springbook.service.UserService;
import jakarta.servlet.http.HttpSession;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import static com.lzq.springbook.constant.Constant.SESSION_USER_KEY;

@Slf4j
@RequestMapping("/user")
@RestController
public class UserController {
    @Autowired
    private UserService userService;
    @RequestMapping("/login")
    public Boolean login(String userName, String password, HttpSession session) {
        if(!StringUtils.hasLength(userName) || !StringUtils.hasLength(password)) {
            log.error("接收到为空的参数");
            return false;
        }
        log.info("login,userName,password: {},{}",userName,password);
        UserInfo userInfo = userService.queryUserByNameService(userName);
        log.info("login,userInfo: {}",userInfo);
        if(userInfo==null) return false;
        if(password.equals(userInfo.getPassword())) {
            session.setAttribute(SESSION_USER_KEY,userInfo);
            log.info("session设置成功");
            return true;
        }
        return false;
    }
}
